package com.example.Android_test.util;

import android.media.MediaPlayer;
import android.util.Log;
import com.example.Android_test.activity.Main;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 03.06.13
 * Time: 13:32
 * To change this template use File | Settings | File Templates.
 */
public class PlaySound {
    private static final int DELAY = 300;
    private String soundResName;
    private boolean withDelay;


    public PlaySound(String soundResName) {
        this(soundResName, false);
    }

    public PlaySound(String soundResName, boolean withDelay) {
        this.soundResName = soundResName;
        this.withDelay = withDelay;
    }

    public void play() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (withDelay) {
                    try {
                        Thread.sleep(DELAY);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    int soundId = Main.getAppContext().getResources().getIdentifier(soundResName, "raw", Main.getAppContext().getPackageName());
                    Log.d(Main.LOG_TAG, "SOUND ID = " + soundId);

                    MediaPlayer mediaPlayer = MediaPlayer.create(Main.getAppContext(), soundId);
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.w(Main.LOG_TAG, "soundRes not found name = " + soundResName);
                }
            }
        });

        thread.start();
    }
}
